import type { ColumnProps } from "ant-design-vue/es/table";

/**
 * 表格选择
 */
const useTableSelect = () => {
  type Key = ColumnProps["key"];

  const selectId = ref<number>(0);
  const selectedRowKeys = ref<Key[]>([]);
  const selectedRows = ref<any[]>([]);
  const hasSelected = computed(() => selectedRowKeys.value.length > 0);
  const hasSelectOne = computed(() => selectedRowKeys.value.length == 1);
  const onSelectChange = (keys: Key[], rows: Object[]) => {
    selectedRowKeys.value = keys;
    selectedRows.value = rows;
    if (keys.length == 1) {
      selectId.value = Number(keys[0]);
    } else {
      selectId.value = 0;
    }
  };

  return {
    selectId,
    selectedRowKeys,
    selectedRows,
    hasSelected,
    hasSelectOne,
    onSelectChange
  };
};

export default useTableSelect;
